package com.incross.dawin.http.parser;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;

import com.incross.dawin.util.BKLog;


public class AdditionalInfoParser {
	// 20141007 다윈 고도화로 인해 추가된 파생광고 및 기타 정보를 파싱하는 파서
	private boolean mIsTickerAvailable = false;
	private boolean mIsSnsShareAvailable = false;
	private boolean mIsAdditionalInfoAvailable = false;
	
	private ArrayList<SnsShare> mSnsShareList;
	private ArrayList<TickerAd> mTickerList;
	
	public AdditionalInfoParser() {
		
	}
	
	
	public void initSnsShareInfo(NodeList snsShare) {
		BKLog.i("- sns init -");
		try { // 20141216 파싱오류 발생시 sns 공유 없음으로 설정하고 그대로 진행
			mSnsShareList = new ArrayList<SnsShare>();
			NodeList snsShareList = snsShare.item(0).getChildNodes();
			
			for(int i = 0; i < snsShareList.getLength(); i++) {
				if(snsShareList.item(i).getFirstChild() != null) {
					NamedNodeMap attributes = snsShareList.item(i).getAttributes();
					
					String program = "";
					String clickTh = "";
					String clickTr = "";
					String iconUrl = "";
					String message = "";
					
					for(int x = 0; x < attributes.getLength(); x++) {
						if(attributes.item(x).getNodeName().equals("program")) {
							program = attributes.item(x).getNodeValue();
						}
					}
					
					if(program != null && !program.equals("")) {
						NodeList details = snsShareList.item(i).getChildNodes();
						
						for(int j = 0; j < details.getLength(); j++) {
							if(details.item(j).getFirstChild() != null) {
								if(details.item(j).getNodeName().equals("StaticResource")) {
									iconUrl = details.item(j).getFirstChild().getNodeValue();
								} else if(details.item(j).getNodeName().equals("Message")) {
									message = details.item(j).getFirstChild().getNodeValue();
								} else if(details.item(j).getNodeName().equals("SnsShareIconClicks")) {
									NodeList snsClickNode = details.item(j).getChildNodes();
									for(int k = 0; k < snsClickNode.getLength(); k++) {
										if(snsClickNode.item(k).getFirstChild() != null) {
											if(snsClickNode.item(k).getNodeName().equals("SnsShareIconClickThrough")) {
												clickTh = snsClickNode.item(k).getFirstChild().getNodeValue();
											} else if(snsClickNode.item(k).getNodeName().equals("SnsShareIconClickTracking")) {
												clickTr = snsClickNode.item(k).getFirstChild().getNodeValue();
											}
										}
									}
								}
								
								if(program != null && !program.equals("") &&
									clickTh != null && !clickTh.equals("") &&
									clickTr != null && !clickTr.equals("") &&
									iconUrl != null && !iconUrl.equals("") &&
									message != null && !message.equals("")) {
									mSnsShareList.add(new SnsShare(program, clickTh, clickTr, iconUrl, message));
									
									BKLog.d("sns name : " + program);
									BKLog.d("click through : " + clickTh);
									BKLog.d("click tracking : " + clickTr);
									BKLog.d("icon utl : " + iconUrl);
									BKLog.d("message : " + message);
									BKLog.d("##################################################################################################");
								}
							}
						}
					}
				}
			}
			
			if(mSnsShareList.size() > 0) {
				mIsSnsShareAvailable = true;
				mIsAdditionalInfoAvailable = true;
			}
		} catch(Exception e) {
			e.printStackTrace();
			mIsSnsShareAvailable = false;
		}
	}
	
	public void initTickerInfo(NodeList ticker) {
		BKLog.i("- ticker init -");
		try { // 20141216 파싱오류 발생시 티커 없음으로 설정하고 그대로 진행
			mTickerList = new ArrayList<TickerAd>();
			NodeList snsShareList = ticker.item(0).getChildNodes();
			
			for(int i = 0; i < snsShareList.getLength(); i++) {
				if(snsShareList.item(i).getFirstChild() != null) {
					NamedNodeMap attributes = snsShareList.item(i).getAttributes();
					
					String program = "";
					int positionNum = -1;
					int startTime = -1;
					int endTime = -1;
					int sizePer = 0;
					String clickTh = "";
					String clickTr = "";
					String imageUrl = "";
					
					for(int x = 0; x < attributes.getLength(); x++) {
						if(attributes.item(x).getNodeName().equals("program")) {
							program = attributes.item(x).getNodeValue();
						} else if(attributes.item(x).getNodeName().equals("positionNumber")) {
							positionNum = Integer.valueOf(attributes.item(x).getNodeValue());
						} else if(attributes.item(x).getNodeName().equals("sizePercent")) {
							sizePer = Integer.valueOf(attributes.item(x).getNodeValue());
						} else if(attributes.item(x).getNodeName().equals("startViewSec")) {
							startTime = Integer.valueOf(attributes.item(x).getNodeValue());
						} else if(attributes.item(x).getNodeName().equals("endViewSec")) {
							endTime = Integer.valueOf(attributes.item(x).getNodeValue());
						}
					}
					
					if(program != null && !program.equals("")) {
						NodeList details = snsShareList.item(i).getChildNodes();
						
						for(int j = 0; j < details.getLength(); j++) {
							if(details.item(j).getFirstChild() != null) {
								if(details.item(j).getNodeName().equals("StaticResource")) {
									imageUrl = details.item(j).getFirstChild().getNodeValue();
								} else if(details.item(j).getNodeName().equals("TickerClickThrough")) {
									clickTh = details.item(j).getFirstChild().getNodeValue();
								} else if(details.item(j).getNodeName().equals("TickerClickTracking")) {
									clickTr = details.item(j).getFirstChild().getNodeValue();
								}
								
								if(program != null && !program.equals("") &&
									clickTh != null && !clickTh.equals("") &&
									clickTr != null && !clickTr.equals("") &&
									imageUrl != null && !imageUrl.equals("") &&
									positionNum > 0 && startTime >= 0 && endTime > 0 && sizePer > 0) {
									mTickerList.add(new TickerAd(program, positionNum, sizePer, startTime, endTime, imageUrl, clickTh, clickTr));
									
									BKLog.d("ticker type : " + program);
									BKLog.d("click through : " + clickTh);
									BKLog.d("click tracking : " + clickTr);
									BKLog.d("image url : " + imageUrl);
									BKLog.d("size percent : " + sizePer);
									BKLog.d("start Time : " + startTime);
									BKLog.d("end time : " + endTime);
									BKLog.d("position : " + positionNum);
									BKLog.d("##########################################################################################################");
								}
							}
						}
					}
				}
			}
			
			// ticker data sort by start show time
			Comparator<TickerAd> comparator = new Comparator<TickerAd>() {
				private final Collator collator = Collator.getInstance();
				@Override
				public int compare(TickerAd lhs, TickerAd rhs) {
					String startTime1 = String.valueOf(rhs.getStartViewTime());
					String startTime2 = String.valueOf(lhs.getStartViewTime());
					
					String tmp1 = "";
					String tmp2 = "";
					
					int length1 = startTime1.length();
					int length2 = startTime2.length();
					
					if(length1 == length2) {
						// do nothing
					} else {
						if(length1 > length2) {
							int gap = length1 - length2;
							for(int i = 0; i < gap; i++) {
								tmp2 = tmp2 + "0";
							}
						} else {
							int gap = length2 - length1;
							for(int i = 0; i < gap; i++) {
								tmp1 = tmp1 + "0";
							}
						}
					}
					return collator.compare(tmp1 + startTime1, tmp2 + startTime2);
				}
			};
			
			/*BKLog.e("ticker count before sort : " + mTickerList.size());
			for(int i = 0; i < mTickerList.size(); i++) {
				BKLog.e("program : " + mTickerList.get(i).getProgram() + ", startTime : " + mTickerList.get(i).getStartViewTime());
			}
			BKLog.e("##############################################################################");*/
			
			Collections.sort(mTickerList, comparator);
			Collections.reverse(mTickerList);
			
			/*BKLog.e("ticker count after sort : " + mTickerList.size());
			for(int i = 0; i < mTickerList.size(); i++) {
				BKLog.e("program : " + mTickerList.get(i).getProgram() + ", startTime : " + mTickerList.get(i).getStartViewTime());
			}*/
			
			if(mTickerList.size() > 0) {
				mIsTickerAvailable = true;
				mIsAdditionalInfoAvailable = true;
			}
		} catch(Exception e) {
			e.printStackTrace();
			mIsTickerAvailable = false;
		}
	}
	
	// getting methods
	public boolean isAdditionalInfoAvailable() {
		return mIsAdditionalInfoAvailable;
	}
	
	public boolean isTickerAvailable() {
		return mIsTickerAvailable;
	}
	
	public boolean isSnsShareAvailable() {
		return mIsSnsShareAvailable;
	}
	
	public ArrayList<SnsShare> getSnsShareList() {
		if(mIsSnsShareAvailable) {
			return mSnsShareList;
		} else {
			return null;
		}
	}
	
	public ArrayList<TickerAd> getTickerAdList() {
		if(mIsTickerAvailable) {
			return mTickerList;
		} else {
			return null;
		}
	}
	
	// obj classes
	public class SnsShare {
		private String snsName = "";
		private String clickThrough = "";
		private String clickTracking = "";
		private String iconUrl = "";
		private String message = "";
		
		public SnsShare(String name, String snsClickThrough, String snsClickTracking, String snsIconUrl, String msg) {
			if(name != null) {
				snsName = name;
			}
			
			if(snsClickThrough != null) {
				clickThrough = snsClickThrough;
			}
			
			if(snsClickTracking != null) {
				clickTracking = snsClickTracking;
			}
			
			if(snsIconUrl != null) {
				iconUrl = snsIconUrl;
			}
			
			if(message != null) {
				message = msg;
			}
		}
		
		public String getSnsName() {
			return snsName;
		}
		
		public String getClickThrough() {
			return clickThrough;
		}
		
		public String getClickTracking() {
			return clickTracking;
		}
		
		public String getIconUrl() {
			return iconUrl;
		}
		
		public String getMessage() {
			return message;
		}
				
	}
	
	public class TickerAd {
		private String program = "";
		private int position = 0;
		private int sizePercent = 0;
		private int startViewSec = -1;
		private int endViewSec = -1;
		private String imageUrl = "";
		private String clickThrough = "";
		private String clickTracking = "";
		
		public TickerAd(String _program, int _position, int _sizePercent, int _startViewSec, int _endViewSec, String _imageUrl, String _clickThrough, String _clickTracking) {
			program = _program;
			position = _position;
			sizePercent = _sizePercent;
			startViewSec = _startViewSec * 1000;
			endViewSec = _endViewSec * 1000;
			imageUrl = _imageUrl;
			clickThrough = _clickThrough;
			clickTracking = _clickTracking;
		}
		
		public String getProgram() {
			return program;
		}
		
		public int getPosition() {
			return position;
		}
		
		public int getSizePercent() {
			return sizePercent;
		}
		
		public int getStartViewTime() {
			return startViewSec;
		}
		
		public int getEndViewTime() {
			return endViewSec;
		}
		
		public String getImageUrl() {
			return imageUrl;
		}
		
		public String getClickThrough() {
			return clickThrough;
		}
		
		public String getClickTracking() {
			return clickTracking;
		}
	}
	
	
}
